package guard

import (
	"github.com/gin-gonic/gin"
)

type CreateOrderParam struct {
	Channel     int    `json:"channel"`
	Type        int    `json:"type"`
	ProductType int    `json:"product_type"`
	ProductUUID string `json:"product_uuid"`
	Lang        string `json:"-"`
}

type CreateOrderParamGuard struct{}

func CreateOrder() *CreateOrderParamGuard {
	return new(CreateOrderParamGuard)
}

func (a CreateOrderParam) IsAlipay() bool {
	return a.Channel == 0
}

func (a CreateOrderParam) IsWechatPay() bool {
	return a.Channel == 1
}

func (a CreateOrderParam) IsTheme() bool {
	return a.Type == 0
}

func (a CreateOrderParam) IsPlugin() bool {
	return a.Type == 1
}

func (a *CreateOrderParamGuard) Guard(ctx *gin.Context) (interface{}, error) {

	var l CreateOrderParam

	err := ctx.BindJSON(&l)

	if err != nil {
		return nil, err
	}

	l.Lang = ctx.Query("lang")
	if l.Lang == "" {
		l.Lang = "cn"
	}

	return l, nil
}

func GetCreateOrderParam(c *gin.Context) CreateOrderParam {
	return c.MustGet("param").(CreateOrderParam)
}
